#!/usr/bin/env php
<?php
/**************************************************************************
 *
 *   Copyright 2010 American Public Media Group
 *
 *   This file is part of AIR2.
 *
 *   AIR2 is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   AIR2 is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with AIR2.  If not, see <http://www.gnu.org/licenses/>.
 *
 *************************************************************************/

require_once 'app/init.php';
require_once APPPATH.'/../tests/Test.php';
require_once APPPATH.'/../tests/models/TestTank.php';
require_once APPPATH.'/../tests/models/TestSource.php';
require_once 'tank/Discriminator.php';

/**
 * NOTE: these tests are to make sure the tank/tank_sources lock properly
 * when the discriminator runs.  Also makes sure the discriminator won't run
 * on things in the wrong states.
 */

AIR2_DBManager::init();
$conn = AIR2_DBManager::get_master_connection();
plan(27);

$tank = new TestTank();
$tank->tank_user_id = 1;
$tank->tank_type = 'F';
$tank->tank_status = Tank::$STATUS_IMPORTED;
$tank->save();

$s = new TestSource();
$s->save();

$d = new Discriminator();


/**********************
 * Test discriminating by ID
 */
$tank->TankSource[0]->src_id = $s->src_id;
$tank->TankSource[0]->src_last_name = 'testtesttest';
$tank->save();
$tank->clearRelated();

$lock = $d->run($tank->tank_id);
is( $lock, true, 'disc id - lock' );
is( $d->done_count, 1, 'disc id - done count' );

$s->refresh();
$tank->refresh(true);

is( $s->src_last_name, 'testtesttest', 'disc id - src last name' );
is( $tank->TankSource[0]->tsrc_status, TankSource::$STATUS_DONE, 'disc id - tsrc_status' );
is( $tank->tank_status, Tank::$STATUS_DISCRIMINATED, 'disc id - tank_status' );


/**********************
 * Test discriminating by UUID
 */
$tank->TankSource[0]->src_id = null;
$tank->TankSource[0]->src_uuid = $s->src_uuid;
$tank->TankSource[0]->src_last_name = 'test2test2test2';
$tank->TankSource[0]->tsrc_status = TankSource::$STATUS_NEW;
$tank->tank_status = Tank::$STATUS_IMPORTED;
$tank->save();
$tank->clearRelated();
$s->src_last_name = null;
$s->save();

$lock = $d->run($tank->tank_id);
is( $lock, true, 'disc uuid - lock' );
is( $d->done_count, 1, 'disc uuid - done count' );

$s->refresh();
$tank->refresh(true);

is( $s->src_last_name, 'test2test2test2', 'disc uuid - src last name' );
is( $tank->TankSource[0]->tsrc_status, TankSource::$STATUS_DONE, 'disc uuid - tsrc_status' );
is( $tank->tank_status, Tank::$STATUS_DISCRIMINATED, 'disc uuid - tank_status' );


/**********************
 * Test discriminating by username
 */
$tank->TankSource[0]->src_id = null;
$tank->TankSource[0]->src_uuid = null;
$tank->TankSource[0]->src_username = $s->src_username;
$tank->TankSource[0]->src_last_name = 'test3test3test3';
$tank->TankSource[0]->tsrc_status = TankSource::$STATUS_NEW;
$tank->tank_status = Tank::$STATUS_IMPORTED;
$tank->save();
$tank->clearRelated();
$s->src_last_name = null;
$s->save();

$lock = $d->run($tank->tank_id);
is( $lock, true, 'disc username - lock' );
is( $d->done_count, 1, 'disc username - done count' );

$s->refresh();
$tank->refresh(true);

is( $s->src_last_name, 'test3test3test3', 'disc username - src last name' );
is( $tank->TankSource[0]->tsrc_status, TankSource::$STATUS_DONE, 'disc username - tsrc_status' );
is( $tank->tank_status, Tank::$STATUS_DISCRIMINATED, 'disc username - tank_status' );


/**********************
 * Test failing to create a new source (not enough info ... no username)
 */
$tank->TankSource[0]->src_id = null;
$tank->TankSource[0]->src_uuid = null;
$tank->TankSource[0]->src_username = null;
$tank->TankSource[0]->src_last_name = 'test4test4test4';
$tank->TankSource[0]->tsrc_status = TankSource::$STATUS_NEW;
$tank->tank_status = Tank::$STATUS_IMPORTED;
$tank->save();
$tank->clearRelated();
$s->src_last_name = null;
$s->save();

$lock = $d->run($tank->tank_id);
is( $lock, true, 'disc no-info - lock' );
is( $d->error_count, 1, 'disc no-info - error count' );

$s->refresh();
$tank->refresh(true);

isnt( $s->src_last_name, 'test4test4test4', 'disc no-info - src last name' );
is( $tank->TankSource[0]->tsrc_status, TankSource::$STATUS_ERROR, 'disc no-info - tsrc_status' );
ok( strlen($tank->TankSource[0]->tsrc_errors) > 0, 'disc no-info - tsrc_errors' );
is( $tank->tank_status, Tank::$STATUS_DISCRIM_ERRORS, 'disc no-info - tank_status' );


/**********************
 * Test actually creating a new source
 */
$name = $s->src_username;
$tank->TankSource[0]->src_id = null;
$tank->TankSource[0]->src_uuid = null;
$tank->TankSource[0]->src_username = $name;
$tank->TankSource[0]->src_last_name = 'test5test5test5';
$tank->TankSource[0]->tsrc_status = TankSource::$STATUS_NEW;
$tank->tank_status = Tank::$STATUS_IMPORTED;
$tank->save();
$tank->clearRelated();
$s->delete();

$lock = $d->run($tank->tank_id);
is( $lock, true, 'disc new - lock' );
is( $d->done_count, 1, 'disc new - done count' );

$tank->refresh(true);
$s = Doctrine::getTable('TestSource')->findOneBy('src_username', $name);

ok( $s && $s->exists(), 'disc new - exists' );
is( $s->src_last_name, 'test5test5test5', 'disc new - src last name' );
is( $tank->TankSource[0]->tsrc_status, TankSource::$STATUS_DONE, 'disc new - tsrc_status' );
is( $tank->tank_status, Tank::$STATUS_DISCRIMINATED, 'disc new - tank_status' );

$s->delete(); //cleanup

